package com.example.fresh_market.adapter;

import android.support.annotation.NonNull;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.fresh_market.R;
import com.example.fresh_market.enmus.OrderStatus;
import com.example.fresh_market.enmus.RefundStatus;
import com.example.fresh_market.request.AdmOrderPageResp;

public class OrderAdmAdapter extends ListAdapter<AdmOrderPageResp, OrderAdmAdapter.ViewHolder> {

    private final OnOrderClickListener onOrderClickListener;

    public OrderAdmAdapter(OnOrderClickListener onOrderClickListener) {
        super(new OrderDiffCallback());
        this.onOrderClickListener = onOrderClickListener;
    }

    protected OrderAdmAdapter(@NonNull DiffUtil.ItemCallback<AdmOrderPageResp> diffCallback, OnOrderClickListener onOrderClickListener) {
        super(diffCallback);
        this.onOrderClickListener = onOrderClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.order_list_admin_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        AdmOrderPageResp order = getItem(i);
        viewHolder.bindBean(order);
        viewHolder.handleRefund.setOnClickListener(v -> {
            onOrderClickListener.onHandleRefundClick(v, getItem(viewHolder.getAdapterPosition()));
        });
        viewHolder.deliver.setOnClickListener(v -> {
            onOrderClickListener.onDeliverClick(v, getItem(viewHolder.getAdapterPosition()));
        });
    }

    public AdmOrderPageResp getItem(int position) {
        return super.getItem(position);
    }

    public interface OnOrderClickListener {
        void onDeliverClick(View view, AdmOrderPageResp order);

        void onHandleRefundClick(View view, AdmOrderPageResp order);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView orderId;
        private final TextView createTime;
        private final TextView payType;
        private final TextView payPrice;
        private final TextView payStatus;
        private final TextView prodNum;
        private final Button handleRefund;
        private final Button deliver;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            orderId = itemView.findViewById(R.id.order_orderId);
            createTime = itemView.findViewById(R.id.order_createTime);
            payType = itemView.findViewById(R.id.order_payType);
            payPrice = itemView.findViewById(R.id.order_payPrice);
            payStatus = itemView.findViewById(R.id.order_payStatus);
            prodNum = itemView.findViewById(R.id.order_prodNum);
            handleRefund = itemView.findViewById(R.id.handle_refund);
            deliver = itemView.findViewById(R.id.deliver_order);
        }

        protected void bindBean(final AdmOrderPageResp order) {
            orderId.setText(String.format("订单编号：%s", order.getId()));
            createTime.setText(String.format("下单时间：%s", order.getCreateTime()));
            payType.setText(String.format("支付方式：%s", "wechat".equals(order.getPayType()) ? "微信支付" : "其他方式"));
            payPrice.setText(String.format("总价：%s元", order.getPayPrice()));
            prodNum.setText(String.format("商品数量：%s", order.getTotalNum()));
            if (order.getRefundStatus() != RefundStatus.NOT_REFUND.getCode()) {
                payStatus.setText(RefundStatus.getByCode(order.getRefundStatus()).getDesc());
            } else {
                payStatus.setText(OrderStatus.getByCode(order.getStatus()).getDesc());
            }
            if (order.getRefundStatus() != RefundStatus.APPEALING.getCode()) {
                handleRefund.setVisibility(View.INVISIBLE);
            }
            if (order.getStatus() != OrderStatus.WAITING_TO_SEND.getCode()
                    || order.getRefundStatus() != RefundStatus.NOT_REFUND.getCode()) {
                deliver.setVisibility(View.INVISIBLE);
            }
        }
    }

    private static class OrderDiffCallback extends DiffUtil.ItemCallback<AdmOrderPageResp> {
        @Override
        public boolean areItemsTheSame(@NonNull AdmOrderPageResp oldItem, @NonNull AdmOrderPageResp newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull AdmOrderPageResp oldItem, @NonNull AdmOrderPageResp newItem) {
            return oldItem.equals(newItem);
        }
    }
}
